//
//  LcbRequestParam.m
//  lcb
//
//  Created by lcb on 2017/1/23.
//  Copyright © 2017年 liuchengbin. All rights reserved.
//

#import "LcbRequestParam.h"
#import "LcbJson.h"
#import "LcbDevice.h"
@implementation LcbRequestParam
@synthesize url=_url;
@synthesize downloadurl=_downloadurl;
@synthesize requestModel=_requestModel;
@synthesize headers=_headers;
@synthesize bodys=_bodys;

-(instancetype)initWithUrl:(NSString *)url{
    self=[super init];
    if (self) {
        self.url=url;
        _request_timeout=10;
        _cacheLoadType=NOT_USE_CACHE;
        _requestModel=RequestModel_GET;
    }
    return self;
}

-(void)dealloc
{
    if (_headers) {
        [_headers removeAllObjects];
        _headers=nil;
    }
    if (_bodys) {
        [_bodys removeAllObjects];
        _bodys=nil;
    }
}
/**得到url连接*/
-(NSString*)url{
 
    if([_url containsString:[LcbDevice sharedLcbDevice].domain])
    {
        return _url;
    }
    _url = [NSString stringWithFormat:@"%@%@",[LcbDevice sharedLcbDevice].domain,_url];
    return _url;
}

/**获取请求头*/
-(NSMutableDictionary*)headers
{
    if (!_headers) {
         _headers=[[NSMutableDictionary alloc]init];
    }
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    NSString *app_ver = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    [dict setValue:app_ver forKey:@"AppVersion"];
//    [dict setValue:@"application/x-www-form-urlencoded" forKey:@"Contsetent-Type"];
    [_headers addEntriesFromDictionary:dict];
    return _headers;
}
/**
 * 设置单个请求头(会替换对应的key)
 * @param
 */
-(void)setHeader:(NSString*)values withKey:(NSString*)key{
    [self.headers setValue:values forKey:key];
}

-(NSDictionary*)getBodys{
    return _bodys;
}

/**获取请求体*/
-(NSMutableDictionary*)bodys{
    if (!_bodys) {
        _bodys=[[NSMutableDictionary alloc]init];
    }
    return _bodys;
}
/**
 * 添加请求参数(健值对方式)
 * @param values
 * @param key
 */
-(void)addBody:(NSString*)values withKey:(NSString*)key{
    [self.bodys setValue:values forKey:key];
}

/**
 * 添加请求方式(字典方式)
 * @param bodys
 */
-(void)addBodys:(NSDictionary*)bodys{
    if(!bodys)return;
    [self.bodys setDictionary:bodys];
}

/**
 * 添加请求方式(普通对象方式)
 *
 */
-(void)addBodyOfObject:(id)bodys{
    if(!bodys)return;
    [self addBodys:[LcbJson objectToDictionary:bodys]];
}
@end
